package com.evzgaga.evzrecon.annotations.runtime;

import com.evzgaga.evzrecon.annotations.CallbackHandler;
import com.evzgaga.evzrecon.util.CallbackInfo;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AbstractMethodAnnotationRuntimeAnalyzer {
    protected List<Method> findMethodsByAnnotation(Class classToScan, Class annotationToFind) {
        List<Method> foundMethods = new ArrayList<Method>();
        
        for (Method method : classToScan.getMethods()) {
            if (method.getAnnotation(annotationToFind) != null) {
                foundMethods.add(method);
            }
        }

        return foundMethods;
    }
    
    public Map<String, CallbackInfo> process(Object obj) {
        List<Method> interestingMethods = findMethodsByAnnotation(obj.getClass(), CallbackHandler.class);
        Map<String, CallbackInfo> callbacks = new HashMap<String, CallbackInfo>();
        
        for (Method method : interestingMethods) {
            CallbackInfo info = new CallbackInfo(method, obj);
            postprocessCallbackInfo(info, callbacks);
        }
        
        return callbacks;
    }

    protected abstract void postprocessCallbackInfo(CallbackInfo info, Map<String, CallbackInfo> callbacks);
}
